<template>
  <view class="container">
    <!-- 头部标题 -->
    <view class="header">
      <text class="title">令讯OA管理系统</text>
    </view>
    <!-- 轮播图区域 -->
    <view class="swiper-container">
      <wd-swiper :list="swiperList" autoplay v-model:current="current" class="custom-swiper"></wd-swiper>
    </view>

    <!-- 企业理念展示区域 -->
    <view class="motto-section">
      <view class="motto-card">
        <view class="motto-icon">
          <wd-icon name="lightbulb" color="#FFD700" size="32rpx" />
        </view>
        <view class="motto-content">
          <text class="motto-text">技术塑造企业优势，创新引领行业未来</text>
          <view class="motto-decoration"></view>
        </view>
      </view>
    </view>
    <!-- 功能按钮网格 -->
    <view class="function-section">

      <view class="button-grid">
        <view
          class="button-item"
          v-for="(item, index) in buttonList"
          :key="index"
          @click="handleButtonClick(item)"
          :class="{ 'button-active': activeButton === index }"
          @touchstart="setActiveButton(index)"
          @touchend="clearActiveButton"
        >
          <view class="icon-wrapper" :style="{ background: `linear-gradient(135deg, ${item.color}20, ${item.color}10)` }">
            <wd-icon :name="item.icon" :color="item.color" size="48rpx" />
            <view class="icon-glow" :style="{ background: `${item.color}30` }"></view>
          </view>
          <text class="button-text">{{ item.title }}</text>
          <view class="button-ripple"></view>
          <!-- 角标移动到按钮容器的右上角 -->
          <view
            v-if="item.showBadge && item.badgeCount > 0"
            class="badge"
          >
            {{ item.badgeCount > 99 ? '99+' : item.badgeCount }}
          </view>
        </view>
      </view>
    </view>

  </view>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useUserStore } from '@/store/modules/user';
// 导入图片
import a2Image from '@/pagesA/assets/images/a2.jpg'
import a6Image from '@/pagesA/assets/images/a6.jpg'
import b3Image from '@/pagesA/assets/images/b3.jpg'
// 导入 API
import { pageByTaskWait } from '@/pagesA/api/workflow/task'
import type { TaskQuery } from '@/pagesA/api/workflow/task/types';
import { onShow } from '@dcloudio/uni-app';

const current = ref<number>(1)
const activeButton = ref<number | null>(null)

const userStore = useUserStore();

// 获取待办数量
const getPendingTaskCount = async () => {
  try {
    const response = await pageByTaskWait({
      pageNum: 1,
      pageSize: 1 // 只需要获取总数，不需要具体数据
    });

    // 更新待办按钮的角标数量
    const query: TaskQuery = { pageNum: 1, pageSize: 10 };
    const todoButton = buttonList.value.find(item => item.path === '/pagesA/workflow/taskwaiting');
    const waitRes = await Promise.resolve(pageByTaskWait(query));
    if (todoButton) {
      // 使用数组长度作为待办数量
      todoButton.badgeCount = waitRes.total || 0;
    }
  } catch (error) {
    console.error('获取待办数量失败:', error);
    // 如果获取失败，设置一个测试数量用于演示
    const todoButton = buttonList.value.find(item => item.path === '/pagesA/workflow/taskwaiting');
    if (todoButton) {
      todoButton.badgeCount = 5; // 测试数量
    }
  }
};

const swiperList = ref([
  a2Image,
  a6Image,
  b3Image
])
// 按钮配置数据
const buttonList = ref([
  {
    title: '起草申请',
    icon: 'edit',
    color: '#4A90E2',
    path: '/pagesA/workflow/tasksubmit'
  },
  {
    title: '我的申请',
    icon: 'list',
    color: '#50C878',
    path: '/pagesA/workflow/mydocument'
  },
  {
    title: '我的待办',
    icon: 'clock',
    color: '#FF6B6B',
    path: '/pagesA/workflow/taskwaiting',
    showBadge: true,
    badgeCount: 0
  },
  {
    title: '我的已办',
    icon: 'user-circle',
    color: '#9B59B6',
    path: '/pagesA/workflow/taskfinish'
  }
])

// 按钮交互处理
const setActiveButton = (index: number) => {
  activeButton.value = index
}

const clearActiveButton = () => {
  activeButton.value = null
}

// 按钮点击处理
const handleButtonClick = (item: any) => {
  if (item.path === '/pages/mine/index') {
    // 跳转到个人中心页面
    uni.switchTab({
      url: item.path
    })
    return
  }
  // 这里可以添加页面跳转逻辑
  uni.navigateTo({
    url: item.path
  })
}
onShow(async () => {
  // 获取用户信息
  await getPendingTaskCount()
})
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40rpx 30rpx;
  position: relative;
  overflow-x: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
    animation: float 20s ease-in-out infinite;
    pointer-events: none;
  }
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  33% { transform: translate(30rpx, -30rpx) rotate(120deg); }
  66% { transform: translate(-20rpx, 20rpx) rotate(240deg); }
}

.header {
  text-align: center;
  margin-bottom: 60rpx;
  position: relative;
  z-index: 2;

  .title {
    display: block;
    font-size: 52rpx;
    font-weight: 700;
    color: #ffffff;
    margin-top: 40rpx;
    margin-bottom: 20rpx;
    text-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.3);
    letter-spacing: 2rpx;

    &::after {
      content: '';
      display: block;
      width: 80rpx;
      height: 4rpx;
      background: linear-gradient(90deg, transparent, #ffffff, transparent);
      margin: 20rpx auto 0;
      border-radius: 2rpx;
    }
  }

  .subtitle {
    display: block;
    font-size: 28rpx;
    color: rgba(255, 255, 255, 0.8);
  }
}

/* 轮播图区域 */
.swiper-container {
  margin-bottom: 40rpx;
  border-radius: 20rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.2);

  .custom-swiper {
    border-radius: 20rpx;
    overflow: hidden;
  }
}

/* 企业理念区域 */
.motto-section {
  margin-bottom: 50rpx;

  .motto-card {
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(20rpx);
    border-radius: 20rpx;
    padding: 30rpx;
    border: 1rpx solid rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    gap: 20rpx;
    position: relative;
    overflow: hidden;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
      animation: shimmer 3s infinite;
    }

    .motto-icon {
      width: 60rpx;
      height: 60rpx;
      background: rgba(255, 215, 0, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }

    .motto-content {
      flex: 1;
      position: relative;

      .motto-text {
        color: #ffffff;
        font-size: 28rpx;
        font-weight: 500;
        line-height: 1.6;
        text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
        user-select: text;
        -webkit-user-select: text;

        /* 选中文本的样式 */
        &::selection {
          background: rgba(255, 215, 0, 0.3);
          color: #ffffff;
        }

        &::-moz-selection {
          background: rgba(255, 215, 0, 0.3);
          color: #ffffff;
        }
      }

      .motto-decoration {
        width: 100%;
        height: 2rpx;
        background: linear-gradient(90deg, #FFD700, transparent);
        margin-top: 15rpx;
        border-radius: 1rpx;
      }
    }
  }
}

@keyframes shimmer {
  0% { left: -100%; }
  100% { left: 100%; }
}

/* 功能区域 */
.function-section {
  position: relative;
  z-index: 2;

  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 40rpx;

    .title-text {
      color: #ffffff;
      font-size: 36rpx;
      font-weight: 600;
      margin-right: 20rpx;
      text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
    }

    .title-line {
      flex: 1;
      height: 2rpx;
      background: linear-gradient(90deg, rgba(255,255,255,0.5), transparent);
      border-radius: 1rpx;
    }
  }
}

.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30rpx;
  max-width: 600rpx;
  margin: 0 auto;
}

.button-item {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 28rpx;
  padding: 50rpx 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.15);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(20rpx);
  border: 1rpx solid rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
  cursor: pointer;

  /* 角标样式 */
  .badge {
    position: absolute;
    top: 8rpx;
    right: 8rpx;
    min-width: 36rpx;
    height: 36rpx;
    background: linear-gradient(135deg, #ff4757 0%, #ff3838 100%);
    color: white;
    border-radius: 18rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22rpx;
    font-weight: 600;
    padding: 0 10rpx;
    box-shadow: 0 4rpx 12rpx rgba(255, 71, 87, 0.4);
    border: 2rpx solid white;
    z-index: 10;
    animation: badge-pulse 2s infinite;
  }

  /* 按钮波纹效果 */
  .button-ripple {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.4);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
    pointer-events: none;
  }

  /* 悬停效果 */
  &:hover {
    transform: translateY(-8rpx);
    box-shadow: 0 16rpx 48rpx rgba(0, 0, 0, 0.2);

    .icon-wrapper {
      transform: scale(1.1);

      .icon-glow {
        opacity: 1;
        transform: scale(1.5);
      }
    }
  }

  /* 激活状态 */
  &.button-active,
  &:active {
    transform: translateY(-4rpx) scale(0.98);
    box-shadow: 0 12rpx 36rpx rgba(0, 0, 0, 0.25);

    .button-ripple {
      width: 200rpx;
      height: 200rpx;
    }

    .icon-wrapper {
      transform: scale(1.05);
    }
  }

  /* 按钮文本选中样式 */
  .button-text {
    user-select: text;
    -webkit-user-select: text;

    &::selection {
      background: rgba(102, 126, 234, 0.3);
      color: #333333;
    }

    &::-moz-selection {
      background: rgba(102, 126, 234, 0.3);
      color: #333333;
    }
  }
}

.icon-wrapper {
  width: 100rpx;
  height: 100rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20rpx;
  box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;

  /* 图标光晕效果 */
  .icon-glow {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60rpx;
    height: 60rpx;
    border-radius: 50%;
    transform: translate(-50%, -50%) scale(0);
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
  }


}

.button-text {
  font-size: 28rpx;
  font-weight: 600;
  color: #333333;
  text-align: center;
  line-height: 1.4;
  transition: color 0.3s ease;
}

/* 响应式设计 */
@media screen and (max-width: 750rpx) {
  .container {
    padding: 30rpx 20rpx;
  }

  .header .title {
    font-size: 44rpx;
  }

  .motto-section .motto-card {
    padding: 25rpx;

    .motto-icon {
      width: 50rpx;
      height: 50rpx;
    }

    .motto-content .motto-text {
      font-size: 26rpx;
    }
  }

  .function-section .section-title .title-text {
    font-size: 32rpx;
  }

  .button-grid {
    gap: 25rpx;
    max-width: 100%;
  }

  .button-item {
    padding: 40rpx 25rpx;
    border-radius: 24rpx;

    &:hover {
      transform: translateY(-4rpx);
    }

    .badge {
      top: 6rpx;
      right: 6rpx;
      min-width: 32rpx;
      height: 32rpx;
      font-size: 20rpx;
      border-radius: 16rpx;
    }
  }

  .icon-wrapper {
    width: 80rpx;
    height: 80rpx;

    .icon-glow {
      width: 50rpx;
      height: 50rpx;
    }
  }

  .button-text {
    font-size: 26rpx;
  }
}

/* 超小屏幕适配 */
@media screen and (max-width: 600rpx) {
  .button-grid {
    grid-template-columns: 1fr;
    gap: 20rpx;
  }

  .button-item {
    padding: 35rpx 20rpx;

    .badge {
      top: 4rpx;
      right: 4rpx;
      min-width: 28rpx;
      height: 28rpx;
      font-size: 18rpx;
      border-radius: 14rpx;
    }
  }
}

/* 文本选择全局样式优化 */
::selection {
  background: rgba(255, 215, 0, 0.3);
  color: inherit;
}

::-moz-selection {
  background: rgba(255, 215, 0, 0.3);
  color: inherit;
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 8rpx;
}

::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4rpx;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 4rpx;

  &:hover {
    background: rgba(255, 255, 255, 0.5);
  }
}

/* 角标脉冲动画 */
@keyframes badge-pulse {
  0% {
    transform: scale(1);
    box-shadow: 0 4rpx 12rpx rgba(255, 71, 87, 0.4);
  }
  50% {
    transform: scale(1.1);
    box-shadow: 0 6rpx 16rpx rgba(255, 71, 87, 0.6);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 4rpx 12rpx rgba(255, 71, 87, 0.4);
  }
}
</style>